#include "serverwindow.h"
#include "ui_serverwindow.h"
#include <QMessageBox>

ServerWindow::ServerWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ServerWindow)
{
    ui->setupUi(this);
    init();
}

ServerWindow::~ServerWindow()
{
    delete ui;
}

void ServerWindow::init(){
    setWindowTitle("服务端");
    setGeometry(800, 200, 800, 600);

    label =  new QLabel(this);
    label->setGeometry(400, 50, 80, 30);
    label->setText("已连接：");

    textEdit = new QPlainTextEdit(this);
    textEdit->setGeometry(400, 80, 300, 100);

    receiveMsgLabel = new QLabel(this);
    receiveMsgLabel->setGeometry(100, 170, 80, 30);
    receiveMsgLabel->setText("收到：");

    receiveTextEdit = new QPlainTextEdit(this);
    receiveTextEdit->setGeometry(100, 200, 600, 100);

    sendMsgLabel = new QLabel(this);
    sendMsgLabel->setGeometry(100, 320, 80, 30);
    sendMsgLabel->setText("发送：");

    sendTextEdit = new QPlainTextEdit(this);
    sendTextEdit->setGeometry(100, 350, 600, 100);

    sendBtn = new QPushButton(this);
    sendBtn->setText("发送");
    sendBtn->setGeometry(100,470, 80, 30);
    QObject::connect(sendBtn, SIGNAL(clicked()), this, SLOT(slotSendMsg()));


    startServerBtn =  new QPushButton(this);
    startServerBtn->setGeometry(100, 60, 80, 30);
    startServerBtn->setText("开启服务端");

    stopServerBtn = new QPushButton(this);
    stopServerBtn->setGeometry(100, 100, 80, 30);
    stopServerBtn->setText("停止服务端");
    stopServerBtn->setEnabled(false);

    QObject::connect(startServerBtn, SIGNAL(clicked()), this,SLOT(startServer()));
    QObject::connect(stopServerBtn, SIGNAL(clicked()), this, SLOT(stopServer()));


}
//开启服务端
void ServerWindow::startServer(){
    if(server == nullptr){
         server = new Server(8088, this);
    }
    if(server->startListen()){
        startServerBtn->setEnabled(false);
        stopServerBtn->setEnabled(true);
        //事件绑定
        QObject::connect(server, SIGNAL(signalNewConnection(QString)), this,SLOT(slotNewConnection(QString)));
        QObject::connect(server, SIGNAL(signalReceiveData(QString, QString)), this,SLOT(slotReceiveMsg(QString, QString)));
        QObject::connect(server, SIGNAL(signalDisconnection(QString)), this,SLOT(slotDisConnection(QString)));
    }


}
//停止服务端口
void ServerWindow::stopServer(){
      server->close();
      startServerBtn->setEnabled(true);
      stopServerBtn->setEnabled(false);
}
//发送消息
void ServerWindow::slotSendMsg(){
    ClientSocket* socket = server->getCurrentClientSocket();
    if(socket != nullptr && socket->isOpen()){
         QString s = sendTextEdit->toPlainText();
         socket->write(s.toUtf8().data());
         sendTextEdit->clear();
    }else{
        QMessageBox::warning(this, "警告", "无客户端连接");
    }
}
//收到消息
void ServerWindow::slotReceiveMsg(QString client, QString msg){

    //receiveTextEdit->setPlainText(client.append(":").append(msg));
    //receiveTextEdit->clear();
    receiveTextEdit->appendPlainText(client.append(":").append(msg));
}
//新的连接
void ServerWindow::slotNewConnection(QString client){
    textEdit->appendPlainText(client);
}
//连接断开
void ServerWindow::slotDisConnection(QString client){
   QMessageBox::about(this, "提示", client+"已断开连接");
   QString s = textEdit->toPlainText();
   s = s.replace(client,"");
   textEdit->setPlainText(s);
}
